<template>
  <div class="employees-container">
    <div class="app-container">
      <page-tools>
        <template #left>
          <span>总共: {{ total }} 条</span>
        </template>
        <template #right>
          <el-button round size="small" type="warning" @click="$router.push('employees/import')">导入 Excel</el-button>
          <el-button round size="small" type="danger">导出 Excel</el-button>
          <el-button round size="small" type="primary" @click="showDialog=true">新增员工</el-button>
        </template>
      </page-tools>
      <el-card style="margin-top: 20px">
        <el-table :data="employees">
          <el-table-column label="序号" type="index" />
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="头像">
            <template v-slot="{ row }">
              <img class="avatar" :src="row.staffPhoto" alt="">
            </template>
          </el-table-column>
          <el-table-column label="工号" prop="workNumber" />
          <el-table-column label="聘用形式">
            <template v-slot="{row}">
              {{ formatEmployment(row.formOfEmployment) }}
            </template>
          </el-table-column>
          <el-table-column label="部门" prop="departmentName" />
          <!-- <el-table-column label="入职时间" sortable prop="timeOfEntry" /> -->
          <el-table-column sortable label="入职时间">
            <template v-slot="{ row }">
              {{ formatDate(row.timeOfEntry) }}
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template v-slot="{row}">
              <el-button type="text" size="small">查看</el-button>
              <el-button type="text" size="small">分配角色</el-button>
              <el-button type="text" size="small" @click="hDel(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <div style="margin-top: 10px;height: 60px;">
        <el-pagination
          :current-page="q.page"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="q.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="hSizeChange"
          @current-change="hCurrentChange"
        />
      </div>
      <el-dialog
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        title="新增员工"
        width="45%"
        :visible.sync="showDialog"
        @closed="hClosed"
      >
        <emp-dialog ref="empDialog" @close="showDialog = false" @success="hSuccess" />
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { delEmployee, getEmployees } from '@/api/employees'
import employeesConst from '@/constant/employees'
import EmpDialog from './empDialog.vue'
import dayjs from 'dayjs'
export default {
  components: {
    EmpDialog
  },
  data() {
    return {
      employees: [],
      total: 0,
      q: {
        page: 1,
        size: 10
      },
      showDialog: false
    }
  }, created() {
    this.loadEmployees()
  },
  methods: {
    async  loadEmployees() {
      const res = await getEmployees(this.q)
      // console.log(res)
      this.employees = res.data.rows
      this.total = res.data.total
    },
    formatEmployment(code) {
      const result = employeesConst.hireType.find(item => item.id === code)
      if (result) {
        return result.value
      } else {
        return '未知'
      }
    },
    hSizeChange(size) {
      this.q.size = size
      this.loadEmployees()
    },
    hCurrentChange(page) {
      this.q.page = page
      this.loadEmployees()
    },
    async hDel(id) {
      try {
        // 提醒用户
        await this.$confirm('真的要删除吗？', '提示', { type: 'warning' })
        // 发请求
        const res = await delEmployee(id)
        // 提示用户
        this.$message.success(res.message)
        // 修复页码显示不正常
        if (this.employees.length === 1 && this.q.page > 1) {
          this.q.page--
        }
        // 重新加载列表
        this.loadEmployees()
      } catch (e) {
        // 精细化处理异常
        if (e === 'cancel') return
        this.$message.warning(e.message)
      }
    },
    hSuccess() {
      // 关闭 dialog
      this.showDialog = false
      // 判断最后一页是否满了
      // if (this.total % this.q.size === 0) {
      //   this.total++
      // }
      this.total++
      // 算出最后一页赋值给 this.q.page
      this.q.page = Math.ceil(this.total / this.q.size)
      // 发请求获取新的数据
      this.loadEmployees()
    },
    formatDate(date) {
      return dayjs(date).format('YYYY-MM-DD')
    },
    hClosed() {
      // 清空子组件的表单
      this.$refs.empDialog.resetForm()
    }
  }
}
</script>
<style lang="scss" scoped>
.avatar {
  width: 50px;
  height: 50px;
  border-radius: 50%;
}
.el-pagination{
  display: flex;
  justify-content: center;
  align-content: center;
}
</style>
